package beautiful.butterfly.schedule.strategy;

import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;

@Slf4j
class MachineInfoTimerTask extends TimerTask {

    ScheduleFactory scheduleFactory;
    int count = 0;

    public MachineInfoTimerTask(ScheduleFactory scheduleFactory) {
        this.scheduleFactory = scheduleFactory;
    }

    public void run() {
        try {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            if (this.scheduleFactory.zooKeeperManager.zooKeeperIsConnected() == false) {
                if (count > 5) {
                    this.scheduleFactory.reStart();
                } else {
                    count = count + 1;
                }
            } else {
                count = 0;
                this.scheduleFactory.refresh();
            }

        } catch (Throwable ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            scheduleFactory.timerTaskHeartBeatTS = System.currentTimeMillis();
        }
    }
}
